home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / CONVERTR / RTF2HTML / SRC / RTF2HTML.TAR / rtftohtml_src / Libs / lib-unix / fileops.c next >
Encoding:
C/C++ Source or Header  |  1994-11-05  |  1.7 KB  |  87 lines

  1. /*
  2.  * fileops.c -- file operations for UNIX versions of translators.
  3.  */
  4. /*
  5.  * UnixOpenLibFile () - Open a library file.  Looks in the following
  6.  * directories:
  7.  * - Looks in current directory.
  8.  * - if RTFLIBDIR environment variable is set, looks in directory named by it.
  9.  * - Looks in executable program's directory, if UnixSetProgPath() has
  10.  * been called.
  11.  * - Looks in library directory, LIBDIR.
  12.  *
  13.  * Exception: if file is an absolute pathname, look only for file as named.
  14.  *
  15.  * Returns NULL if file cannot be found and opened.
  16.  */
  17.  
  18. # include    <stdio.h>
  19.  
  20. # include    "rtf.h"
  21. # include    "rtf-unix.h"
  22.  
  23. extern char    *getenv ();
  24.  
  25. static char    *progPath = (char *) NULL;
  26.  
  27.  
  28. void
  29. UnixSetProgPath (path)
  30. char    *path;
  31. {
  32. int    i, j, n;
  33.  
  34.     n = strlen (path);
  35.     for (j = -1, i = 0; i < n; i++)
  36.     {
  37.         if (path[i] == '/')
  38.             j = i;
  39.     }
  40.     if (j < 0)        /* no slash found */
  41.     {
  42.         path = ".";
  43.         j = 1;
  44.     }
  45.     if ((progPath = RTFAlloc (j + 1)) != (char *) NULL)
  46.     {
  47.         (void) strncpy (progPath, path, j);
  48.         progPath[j] = '\0';
  49.     }
  50. }
  51.  
  52.  
  53. FILE *
  54. UnixOpenLibFile (file, mode)
  55. char    *file;
  56. char    *mode;
  57. {
  58. FILE    *f;
  59. char    buf[rtfBufSiz];
  60. char    *p;
  61.  
  62.     if ((f = fopen (file, mode)) != (FILE *) NULL)
  63.     {
  64.         return (f);
  65.     }
  66.     /* if abolute pathname, give up, else look in library */
  67.     if (file[0] == '/')
  68.     {
  69.         return ((FILE *) NULL);
  70.     }
  71.     if ((p = getenv ("RTFLIBDIR")) != (char *) NULL)
  72.     {
  73.         sprintf (buf, "%s/%s", p, file);
  74.         if ((f = fopen (buf, mode)) != (FILE *) NULL)
  75.             return (f);
  76.     }
  77.     if (progPath != (char *) NULL)
  78.     {
  79.         sprintf (buf, "%s/%s", progPath, file);
  80.         if ((f = fopen (buf, mode)) != (FILE *) NULL)
  81.             return (f);
  82.     }
  83.     sprintf (buf, "%s/%s", LIBDIR, file);
  84.     f = fopen (buf, mode);    /* NULL if it fails */
  85.     return (f);
  86. }
  87.